package com.vip.jwt.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vip.jwt.common.entity.R;
import com.vip.jwt.entity.Menu;
import com.vip.jwt.service.MenuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author vip
 * @since 2021-05-04
 */
@RestController
@RequestMapping("/api/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    @PostMapping
    public Object addMenu(@RequestBody Menu menu){
        menuService.save(menu);
        return R.ok();
    }

    @DeleteMapping("/{id}")
    public Object deleteById(@PathVariable String id){
        menuService.deleteById(id);
        return R.ok();
    }

    @PutMapping
    public Object updateMenu(@RequestBody Menu menu){
        menuService.updateById(menu);
        return R.ok();
    }

    @GetMapping("/tree")
    public Object tree(){
        return R.ok().data("items",menuService.tree());
    }

    @ApiOperation("获取菜单列表（分页）")
    @GetMapping("/{page}/{limit}")
    public Object pageList(@PathVariable Long page,@PathVariable Long limit){
        Page<Menu> menuPage = new Page<>(page,limit);
        Page<Menu> menus = menuService.page(menuPage);
        return R.ok().data("data",menus);
    }

}

